import { Repository } from 'typeorm';
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';

import { Profile } from '../../entity/profile.entity';

@Injectable()
export class ProfileService {
  constructor(
    @InjectRepository(Profile)
    private readonly profileRepository: Repository<Profile>,
  ) {}

  async saveProfile(profile: Profile) {
    return await this.profileRepository.save(profile);
  }

  async updateProfile(profile: Profile) {
    return await this.profileRepository.update(profile.id, profile);
  }

  async getProfileByUserId(userId: Number) {
    return await this.profileRepository
      .createQueryBuilder('profile')
      .leftJoinAndSelect('profile.user', 'user')
      .where('user.id = :id', { id: userId })
      .getOne();
  }
}
